package zero.tech.games.log.queue;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import com.alibaba.fastjson.JSONObject;

import zero.tech.core.util.DatetimeUtil;
import zero.tech.games.common.QueueConsts;
import zero.tech.games.common.ThreadRunner;
import zero.tech.games.common.playback.Playback;
import zero.tech.games.entity.log.LogPlayback;
import zero.tech.games.log.hot.LogPlaybackUtil;
import zero.tech.games.log.shareding.DynamicCreateTableSharding;
import zero.tech.redis.queue.QueueUtil;
@Configuration
@AutoConfigureAfter(DynamicCreateTableSharding.class)
public class LogPlaybackProcess extends ThreadRunner{
	private static final Logger logger=LoggerFactory.getLogger(LogPlaybackProcess.class);
	@Autowired
	private QueueUtil queueUtil;
	@Autowired
	private LogPlaybackUtil logPlaybackUtil;
	@Override
	public void process() {
		
		while(true) {
			int sleep = 0;
			List<String> list = queueUtil.pullQueueBatch(QueueConsts.QUEUE_PLAYBACK, 100);
			try {
				if (list == null || list.isEmpty()) {
					sleep = 1000;
				} else {
					// 开始处理业务
					long time = System.currentTimeMillis();
					logger.info("============================开始战绩回放日志入库==============================");
					
					List<LogPlayback> logPlaybacks=new ArrayList<LogPlayback>();
					for(String str:list) {
						Playback playback = JSONObject.parseObject(str,Playback.class);
						List<LogPlayback> logs = packageLogPlayback(playback);
						logPlaybacks.addAll(logs);
					}
					if(!logPlaybacks.isEmpty()) {
						logPlaybackUtil.insertBatch(logPlaybacks);
					}
					logger.info("=====================结束战绩回放日志入库,操作耗时:{}毫秒========================",
							(System.currentTimeMillis() - time));
				}
			} catch (Exception e) {
				e.printStackTrace();
				sleep = 1000;
			}
			if (sleep != 0) {
				try {
					Thread.sleep(sleep);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
	private List<LogPlayback> packageLogPlayback(Playback playback) {
		List<LogPlayback> list=new ArrayList<LogPlayback>();
		Date date=playback.getCreate_stamp();
		List<Integer> uids=playback.getUids();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", date);
		for(Integer uid:uids) {
			LogPlayback logPlayback=new LogPlayback();
			logPlayback.setUid(uid);
			logPlayback.setClub_id(playback.getClub_id());
			logPlayback.setGame_id(playback.getGame_id());
			logPlayback.setRoom_id(playback.getRoom_id());
			logPlayback.setRoom_times(playback.getRoom_times());
			logPlayback.setPlay_code(playback.getPlay_code());
			logPlayback.setConfig_str(playback.getConfig_str());
			logPlayback.setSmall(JSONObject.toJSONString(playback.getBasics()));
			logPlayback.setContent(JSONObject.toJSONString(playback));
			logPlayback.setUids(JSONObject.toJSONString(playback.getUids()));
			logPlayback.setDay_str(day_str);
			logPlayback.setCreate_stamp(date);
			list.add(logPlayback);
		}
		return list;
	}

}
